<script>
    function submitItemStatusChange(itemIDStr, onSale, toDelete){
        itemID = parseInt(itemIDStr)
        $.ajax({
            type: "POST",
            url: '/submit/change/itemStatus',
            headers: { 'Authorization': `Bearer ${token}` },
            data: {
                'itemID': itemID,
                'onSale': onSale,
                'toDelete': toDelete
            },
            success: function (resJson) {
                if (resJson.success) {
                    // 成功反馈
                    location.reload();
                } else {
                    // ! 从后端接收到错误
                    // 失败反馈
                    alert("更改商品状态失败: "+ resJson.errorInfo)
                }
            }

        })
    }

    function submitUserChange(){
        var portraitFileName = $('#portraitFileName').val()
        var userDescription = $('#userDescription').val()
        var userWeChat = $("#userWeChat").val()
        var userQQ = $("#userQQ").val()
        var token = window.localStorage.getItem('xjtu-idle-token');
        $.ajax({
            type: 'POST',
            url: 'submit/change/userNormalInfo',
            headers: { 'Authorization': `Bearer ${token}` },
            data:{
                portraitFileName: portraitFileName,
                userDescription: userDescription,
                userWeChat: userWeChat,
                userQQ: userQQ
            },
            success: (resJson)=>{
                if(!resJson.success){
                    alert("修改用户信息失败: "+ resJson.errorInfo)
                }else{
                    alert("提交成功!")
                    location.reload()
                }
            }
        })
    }

    function submitItemChange(){
        //清空表单验证状态
        $("input").removeClass("is-invalid");
        // 得到商品ID
        var itemID = $("#itemEditModal").attr('data-item-id')
        // 验证token
        var token = window.localStorage.getItem('xjtu-idle-token');

        file_hw_ratio = item_g.CoverFileHWRatio
        
        //验证表单输入
        // 商品名称
        if ($("#itemName").val().length > 36 || $("#itemName").val() == "") $("#itemName").addClass("is-invalid");
        // 商品价格
        var price = $("#itemPrice").val()
        var re = /^[0-9]+$/
        if (!re.test(price) || parseInt(price) < 0) $("#itemPrice").addClass("is-invalid");
        // 商品简介
        if ($("#itemDescription").val().length > 200 || $("#itemDescription").val() == "") $("#itemDescription").addClass("is-invalid");
        // 新旧程度描述
        if ($("#itemOldNewRate").val().length > 200) $("#itemOldNewRate").addClass("is-invalid");
        // 封面
        if ($("#coverFileName").val() == '') $("#coverFileName").addClass("is-invalid");
        // 备注
        if ($("#remarks").val().length > 200) $("#remarks").addClass("is-invalid")


        if ($("input").hasClass("is-invalid")) {
            alert('存在非法输入, 请检查')
            return;
        }

        submitData = $("form").serializeObject();

        $.ajax({
            type: 'POST',
            url: '/submit/change/item',
            data: { formData: JSON.stringify(submitData), file_hw_ratio: file_hw_ratio, itemID: itemID },
            headers: { 'Authorization': `Bearer ${token}` },
            success: function (resJson) {
                if (!resJson.success) {
                    alert('修改商品信息发生错误: ' + resJson.errorInfo)
                } else {
                    location.reload()
                }
            },
        })
    }
</script>